﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public partial class ZbFromAppraisal2 : System.Web.UI.Page
    {
        private const string HTML_SPACE = "&nbsp;";
        private const string ITEM_CTRL_PREID = "KPItem_";

        protected void Page_Load(object sender, EventArgs e)
        {
            btnPrintTop.Attributes.Add("onclick", "excel(phTemplate);");
            btnPrintBottom.Attributes.Add("onclick", "excel(phTemplate);");

            int nSubID = HelperURL.GetIntFromUrlString("SubID");
            int nKpID = HelperURL.GetIntFromUrlString("KpID");
            int nUmpID = HelperURL.GetIntFromUrlString("UmpID");
            int nDisplay = HelperURL.GetIntFromUrlString("Display");
            bool Display = nDisplay.Equals(1);

            DisplayRating(nSubID, nKpID, nUmpID, Display);
        }


        // 评分
        private void DisplayRating(int nSubID, int nKpID, int nUmpID, bool Display)
        {
            KP_Subject_Table_Ump su = new KP_Subject_Table_Ump();
            su.Where.SubjectID.Value = nSubID;
            su.Where.KpTableID.Value = nKpID;
            su.Where.Status.Value = 0;
            //得到一个评分专家的ID
            su.Query.Load();
            
            VW_ZB_ItemResult itemResult = new VW_ZB_ItemResult();
            itemResult.Where.KpID.Value = nKpID ;
            //itemResult.Where.UmpID.Value = su.UmpID ;
            itemResult.Where.SubID.Value = nSubID ;
            if (!itemResult.Query.Load())
            {
                HelperMessage.Message("请先完成项目初始化！");
                return;
            }

            KP_Subject subject = new KP_Subject();
            subject.LoadByPrimaryKey(itemResult.SubID);
            labKpName.Text = itemResult.KpName;
            labSubName.Text = itemResult.SubName;
            labBidderName.Text = subject.Bidder;
            labEndTime.Text = subject.EndDate.ToString();
            //labUmpName.Text = itemResult.UmpName;

            lbtnSubmit.Enabled = !Display;
            lbtnSubmitBottom.Enabled = !Display;

            phTemplate.Controls.Add(CreateTable(itemResult));

            lbtnSubmit.Attributes.Add(ConstantController.MSG_ALERT_ACTION, ConstantController.MSG_ALERT_SUBMIT_SCRIPT);
            lbtnSubmitBottom.Attributes.Add(ConstantController.MSG_ALERT_ACTION, ConstantController.MSG_ALERT_SUBMIT_SCRIPT);
        }

        public Table CreateTable(VW_ZB_ItemResult vwResult)
        {

            KP_Subject_Employee se = new KP_Subject_Employee();
            ZB_Tender tender = new ZB_Tender();
            string TenderName = "";

            se.Load(vwResult.SubID);
            int nTenderCount = se.RowCount;
            int TableWidth = 700 + nTenderCount * 200;

            //创建表头
            Table tb = new Table();
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();

            //tb.Attributes.Add("style", zTable.TableStyle);
            tb.Attributes.Add("border", "1");
            //tb.Attributes.Add("class", "ms-formlabel");
            tb.Attributes.Add("width", TableWidth + "px");
            tb.Attributes.Add("cellpadding", "0");
            tb.Attributes.Add("cellspacing", "0");
            tb.ID = "dtable";

            KP_Subject_Table_Ump su = new KP_Subject_Table_Ump();
            su.Where.SubjectID.Value = vwResult.SubID;
            su.Where.UmpID.Value = vwResult.UmpID;
            su.Where.KpTableID.Value = vwResult.KpID;
            su.Query.Load();
            int nUmpCount = su.RowCount;

            tr = new TableRow();
            tc = HelperTable.CreateStyleCell(1, 6 + nTenderCount, labKpName.Text+"指标描述", "font-size: 18.0pt;font-weight: 700;font-style: normal;text-decoration: none;font-family: 黑体;text-align: center;vertical-align: middle;white-space: normal;");
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);

            //加入项目信息
            tr = new TableRow();
            KP_Subject subject = new KP_Subject();
            subject.LoadByPrimaryKey(vwResult.SubID);
            int nCol1 = (6 + nTenderCount) / 2;
            int nCol2 = (6 + nTenderCount) - nCol1;
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol1, "项目名称：" + subject.SubjectName, HelperTable.ALIGN_LEFT));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol2, "项目编号：" + subject.Code, HelperTable.ALIGN_RIGHT));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol1, "评标时间：" + DateTime.Now.ToString(), HelperTable.ALIGN_LEFT));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol2, "评标地点：" + subject.Address, HelperTable.ALIGN_RIGHT));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateCell(2, 1, "序号" ));
            tr.Cells.Add(HelperTable.CreateCell(2, 1, "评议指标"));
            tr.Cells.Add(HelperTable.CreateCell(2, 1, "指标内容"));
            tr.Cells.Add(HelperTable.CreateCell(2, 1, "具体要求"));
            tr.Cells.Add(HelperTable.CreateCell(2, 1, "分值"));
            tr.Cells.Add(HelperTable.CreateCell(1, nTenderCount, "投标人"));
            tr.Cells.Add(HelperTable.CreateCell(2, 1, "评分细则"));

            tb.Rows.Add(tr);

            //表头投标公司
            tr = new TableRow();

            se.Rewind();
            while (!se.EOF)
            {
                tender.LoadByPrimaryKey(se.EmployeeID);
                TenderName = tender.Name;
                tr.Cells.Add(HelperTable.CreateCell(1, 1, TenderName));

                se.MoveNext();
            }
            tb.Rows.Add(tr);


            KP_Template template = new KP_Template();
            template.Where.KP_ID.Value = vwResult.KpID;
            template.Where.Is_Group.Value = 0;
            template.Query.Load();
            int nItemCount = template.RowCount;

            template = new KP_Template();
            template.Where.KP_ID.Value = vwResult.KpID;
            template.Where.Is_Group.Value = 1;
            template.Query.Load();
            int nGroupCount = template.RowCount;

            int RowNo = 1;
            while (!su.EOF)
            {
                tr = new TableRow();
                DataRow[] drs = vwResult.DefaultView.Table.Select("UmpID=" + su.UmpID);
                string UmpName = drs[0][VW_ZB_ItemResult.ColumnNames.UmpName].ToString();

                tr.Cells.Add(HelperTable.CreateCell(nItemCount, 1, (RowNo++).ToString()));
                //tr.Cells.Add(HelperTable.CreateCell(nItemCount, 1, UmpName));
                tr.Cells.Add(HelperTable.CreateCell(nItemCount, 1, "指标应答"));

                //添加指标集
                while (!template.EOF)
                {
                    KP_Template itemTemplate = new KP_Template();
                    itemTemplate.Where.Parent_Item_ID.Value = template.Item_ID;
                    itemTemplate.Query.Load();
                    tc = HelperTable.CreateCell(itemTemplate.RowCount, 1, "");
                    //tc.Controls.Add(new LiteralControl(template.Item_Name + "(权重" + (template.Item_Quotiety * 100).ToString() + "%)"));
                    tc.Controls.Add(new LiteralControl(template.Item_Name));

                        //se.Rewind();
                        //while (!se.EOF)
                        //{
                        //    tender.LoadByPrimaryKey(se.EmployeeID);
                        //    TenderName = tender.Name;
                        //    CreateScoreItem(vwResult, su.UmpID, template.Item_ID, se.EmployeeID, tc);

                        //    se.MoveNext();
                        //}

                    tr.Cells.Add(tc);

                    //添加指标项目
                    while (!itemTemplate.EOF)
                    {
                        //tr.Cells.Add(HelperTable.CreateCell(1, 1, itemTemplate.Item_Name + "(权重" + (itemTemplate.Item_Quotiety * 100).ToString() + "%)"));
                        tr.Cells.Add(HelperTable.CreateCell(1, 1, itemTemplate.Item_Name));// + "(权重" + (itemTemplate.Item_Quotiety * 100).ToString() + "%)"));
                        tr.Cells.Add(HelperTable.CreateCell(1, 1, itemTemplate.Max_Score.ToString("N2")));

                        se.Rewind();
                        while (!se.EOF)
                        {
                            DataRow[] items = vwResult.DefaultView.Table.Select("UmpID=" + su.UmpID + " and ItemID= " + itemTemplate.Item_ID + " and TenderID=" + se.EmployeeID);
                            string sScore = items[0][VW_ZB_ItemResult.ColumnNames.ItemScore].ToString();
                            string sText = items[0][VW_ZB_ItemResult.ColumnNames.ItemText].ToString();
                            tc = HelperTable.CreateCell(1, 1, "");

                            TextBox txtItem = new TextBox();
                            int ItemWidth = 200 ;
                            txtItem.ID = ITEM_CTRL_PREID + itemTemplate.Item_ID.ToString() + "_" + se.EmployeeID.ToString();
                            txtItem.ToolTip = se.EmployeeID.ToString();
                            txtItem.Width = Unit.Pixel(ItemWidth);
                            txtItem.Text = sText ;
                            txtItem.TextMode = TextBoxMode.MultiLine;
                            txtItem.Height = Unit.Pixel(54);
 
                            tc.Controls.Add(txtItem);

                            // 添加评分有效性验证控件
                            RequiredFieldValidator validator = new RequiredFieldValidator();
                            validator.ID = "RFV_" + ITEM_CTRL_PREID + itemTemplate.Item_ID.ToString() + "_" + se.EmployeeID.ToString();
                            validator.ControlToValidate = ITEM_CTRL_PREID + itemTemplate.Item_ID.ToString() + "_" + se.EmployeeID.ToString();
                            //validator.ClientValidationFunction = "is_valid_score";
                            validator.ErrorMessage = "请评分项－" + itemTemplate.Item_Name;
                            validator.Text = "*";
                            validator.Display = ValidatorDisplay.Dynamic;
                            tc.Controls.Add(validator);


                            tr.Cells.Add(tc);

                            se.MoveNext();
                        }

                        // 指标项评分描述
                        //tc = new TableCell();
                        //tc.Text = itemTemplate.Item_Desc;
                        tc = HelperTable.CreateCell(1, 1, itemTemplate.Item_Desc);
                        tr.Cells.Add(tc);

                        tb.Rows.Add(tr);
                        tr = new TableRow();

                        itemTemplate.MoveNext();
                    }

                    template.MoveNext();
                }

                su.MoveNext();
            }
            tr = new TableRow();
            //tr.Cells.Add(HelperTable.CreateCell(1, 6+nTenderCount, "评委：____________________________"+vwResult.UmpName));
            tr.Cells.Add(HelperTable.CreateCell(1, 6+nTenderCount, "评委：____________________________"));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateCell(1, 6 + nTenderCount, "湖北信通通信有限公司惠达招标分公司"));
            tb.Rows.Add(tr);

            return tb;

        }

        // 保存结果，bIsSaved = false, 仅计算结果，不保存
        private void DoSave(bool bIsSaved)
        {
            if (!Page.IsValid)
                return;

            if (bIsSaved)
            {
                Save();
                HelperMessage.Message("保存成功");
            }
        }

        private void Save()
        {
            int nSubID = HelperURL.GetIntFromUrlString("SubID");
            int nKpID = HelperURL.GetIntFromUrlString("KpID");

            VW_ZB_ItemResult vw = new VW_ZB_ItemResult();
            vw.Where.KpID.Value = nKpID;
            vw.Where.SubID.Value = nSubID;
            vw.Query.Load();
            while (!vw.EOF)
            {
                KP_Item_Result ir = new KP_Item_Result();
                ir.LoadByPrimaryKey(vw.ResultID);

                string ctrlID = ITEM_CTRL_PREID + vw.ItemID.ToString() + "_" + vw.TenderID;
                Control ctrl = phTemplate.FindControl(ctrlID);
                if (ctrl != null)
                {
                    TextBox tb = (TextBox)ctrl;
                    ir.Item_Text = tb.Text;
                    ir.Save();
                }

                vw.MoveNext();
            }
            //修改评分专家填报状态
            KP_Subject_Table_Ump stu = new KP_Subject_Table_Ump();
            stu.Where.SubjectID.Value = nSubID;
            stu.Where.KpTableID.Value = nKpID;
            stu.Where.UmpType.Value = 1;
            stu.Query.Load();
            stu.Status = 1;
            stu.Save();
        }

        protected void lbtnSubmit_Click(object sender, System.EventArgs e)
        {
            DoSave(true);
        }

        protected void lbtnSubmitBottom_Click(object sender, System.EventArgs e)
        {
            DoSave(true);
        }

        protected void lbtnCompute_Click(object sender, System.EventArgs e)
        {
            DoSave(false);
        }

        protected void lbtnComputeBottom_Click(object sender, System.EventArgs e)
        {
            DoSave(false);
        }
    }
}
